package com.fingard.dsp.web.page.receipt;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.fingard.FGBiz;
import com.fingard.common.utils.json.JSONUtils;
import com.fingard.db.JDBCHelper;
import com.fingard.dsp.web.page.PageBase;

public class ReceiptList extends PageBase {
    @Override
    public void pageLoad() throws Exception {
        httpContext.response.isRtnBytes = false;
        httpContext.response.setContentType("txt", "");
        StringBuilder sbJson = new StringBuilder();
        String respCode = "";
        String respInfo = "";
        Connection conn = null;
        try {
            int qryDate = 0;
            String qryAct = "";
            String reqJson = httpContext.request.httpReceiver.getReqBodyContent();
            HashMap<String, String> reqParams = JSONUtils.json2Map(reqJson);
            if (reqParams.containsKey("qryDate") && reqParams.get("qryDate").length() > 0) {
                qryDate = Integer.parseInt(reqParams.get("qryDate"));
            }
            if (reqParams.containsKey("qryAct")) {
                qryAct = reqParams.get("qryAct");
            }
            ArrayList<Object> paramArray = new ArrayList<Object>();
            String qrySql = "select * from RECEIPTMAPPING";
            if (qryDate > 0 || qryAct.length() > 0) {
                String condSql = "";
                if (qryDate > 0) {
                    condSql += " BUSDATE=?";
                    paramArray.add(qryDate);
                }
                if (qryAct.length() > 0) {
                    if (condSql.length() > 0) {
                        condSql += " and ";
                    }
                    condSql += " OWNACT=?";
                    paramArray.add(qryAct);
                }
                qrySql += " where " + condSql;
            }
            qrySql += " limit 50 offset 0;";
            Object[] qryParams = null;
            if (paramArray.size() > 0) {
                qryParams = paramArray.toArray();
            }
            conn = FGBiz.getLocalDbConn();
            List<Map<String, Object>> dataList = JDBCHelper.execQuery(conn, qrySql, qryParams);
            if (dataList != null && dataList.size() > 0) {
                respCode = "0";
                sbJson.append("{");
                sbJson.append("\"respCode\":" + "\"0\",");
                sbJson.append("\"respInfo\":" + "\"\",");
                sbJson.append("\"dataList\":[");
                sbJson.append(DataMap2Json(dataList));
                sbJson.append("]}");
            } else {
                respCode = "-1";
                respInfo = "查询结果为空";
            }
        } catch (Exception ex) {
            respCode = "-1";
            respInfo = ex.toString();
            FGBiz.limitMsg.exception(this.getClass().getSimpleName(), ex);
        } finally {
            if (conn != null) {
                conn.close();
            }
        }
        if ("-1".equals(respCode)) {
            sbJson = new StringBuilder();
            sbJson.append("{");
            sbJson.append("\"respCode\":" + "\"" + respCode + "\",");
            sbJson.append("\"respInfo\":" + "\"" + respInfo + "\"");
            sbJson.append("}");
        }
        httpContext.response.respMsg = sbJson.toString();
    }

    public static String DataMap2Json(List<Map<String, Object>> pList) {
        StringBuilder sbJson = new StringBuilder();
        for (int i = 0; i < pList.size(); i++) {
            if (sbJson.length() > 0) {
                sbJson.append(",");
            }
            sbJson.append("{");
            Map<String, Object> eachMap = pList.get(i);
            Iterator<String> it = eachMap.keySet().iterator();
            int keyCount = 0;
            while (it.hasNext()) {
                keyCount++;
                if (keyCount > 1) {
                    sbJson.append(",");
                }
                String keyName = it.next();
                sbJson.append("\"" + keyName + "\":\"" + eachMap.get(keyName) + "\"");
            }
            sbJson.append("}");
        }
        return sbJson.toString();
    }
}
